package com.ruoyi.function.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.function.constant.BusinessConstants;
import com.ruoyi.function.domain.vo.ChatRecordVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.function.domain.ChatRecord;
import com.ruoyi.function.service.IChatRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.function.constant.BusinessConstants.dict.user_type_1;

/**
 * -- 聊天记录Controller
 * 
 * @author ruoyi
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/function/record")
public class ChatRecordController extends BaseController
{
    @Autowired
    private IChatRecordService chatRecordService;

    /**
     * 查询-- 聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('function:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatRecord chatRecord)
    {
        startPage();
        List<ChatRecord> list = chatRecordService.selectChatRecordList(chatRecord);
        return getDataTable(list);
    }

    /**
     * 导出-- 聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('function:record:export')")
    @Log(title = "-- 聊天记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatRecord chatRecord)
    {
        List<ChatRecord> list = chatRecordService.selectChatRecordList(chatRecord);
        ExcelUtil<ChatRecord> util = new ExcelUtil<ChatRecord>(ChatRecord.class);
        util.exportExcel(response, list, "-- 聊天记录数据");
    }

    /**
     * 获取-- 聊天记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('function:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(chatRecordService.selectChatRecordById(id));
    }

    /**
     * 新增-- 聊天记录
     */
    @PreAuthorize("@ss.hasPermi('function:record:add')")
    @Log(title = "-- 聊天记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatRecord chatRecord)
    {
        return toAjax(chatRecordService.insertChatRecord(chatRecord));
    }

    /**
     * 修改-- 聊天记录
     */
    @PreAuthorize("@ss.hasPermi('function:record:edit')")
    @Log(title = "-- 聊天记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatRecord chatRecord)
    {
        return toAjax(chatRecordService.updateChatRecord(chatRecord));
    }

    /**
     * 删除-- 聊天记录
     */
    @PreAuthorize("@ss.hasPermi('function:record:remove')")
    @Log(title = "-- 聊天记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(chatRecordService.deleteChatRecordByIds(ids));
    }
    //查询历史记录
    @GetMapping("/getHistory")
    public AjaxResult getHistory(Long senderId,Long receiverId ,String userType){
    	List<ChatRecordVo> history = chatRecordService.getHistory(senderId, receiverId ,userType);
    	return success(history);
    }

    /**
     * 查询当前用户的聊天记录
     * @param currentRole
     * @return
     */
    @GetMapping("/getCurrentUserChatRecords")
    public AjaxResult getCurrentUserChatRecords(Long currentRole) {
        List<ChatRecordVo> chatRecords = chatRecordService.getCurrentUserChatRecords(currentRole);
        return success(chatRecords);
    }

    // 修改为已读
    @GetMapping("/read")
    public AjaxResult read(@RequestBody ChatRecord chatRecord) {

        return toAjax(chatRecordService.read(chatRecord));
    }

    /**
     * 获取未读消息数量
     * @param currentRole
     * @return
     */
    @GetMapping("/getUnreadCount")
    public AjaxResult getUnreadCount(Long currentRole) {
        int count = chatRecordService.getUnreadCount(currentRole);
        return success(count);
    }
}
